package tests;

import common.Utils;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;

public class MessagePage extends Utils {
    private static String url = "http://8.134.99.135:58080/index.html";

    public MessagePage() {
        super(url);
    }

    //未登录状态，直接跳回登录页面
    public void noLogin() {
        String currentUrl = driver.getCurrentUrl();
        Assertions.assertEquals("http://8.134.99.135:58080/sign-in.html", currentUrl);
    }

    //登录状态下
    public void loginSuc(){
        //点击“小铃铛”
        driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light." +
                "d-print-none> div > div > div:nth-child(2) > div > a")).click();

        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#index_message_offcanvasEnd")));

        //1、检查页面是否与预期一样
        String tile = driver.findElement(By.cssSelector("#offcanvasEndLabel")).getText();
        Assertions.assertEquals("所有站内信",tile);
        driver.findElement(By.cssSelector("#index_message_offcanvasEnd > div.offcanvas-header > button"));
        driver.findElement(By.cssSelector("#index_div_message"));
        WebElement clickElement = driver.findElement(By.cssSelector("#index_div_message_list > div > " +
                "div > div:nth-child(3)"));
        wait.until(ExpectedConditions.elementToBeClickable(clickElement));

        String from = driver.findElement(By.cssSelector("#index_div_message_list > div > div > div.col.text-truncate " +
                "> a > span:nth-child(2)")).getText();
        Assertions.assertTrue(from.contains("来自"));
        Assertions.assertTrue(from.contains("的消息"));

        //2、查看私信详情
        clickElement.click();
        driver.findElement(By.cssSelector("#index_message_reply_modal > div > div"));
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#btn_index_message_reply")));
        String title2 = driver.findElement(By.cssSelector("#index_message_detail_title")).getText();
        Assertions.assertTrue(title2.contains("收到来自 "));
        Assertions.assertTrue(title2.contains(" 的新消息"));
        String content = driver.findElement(By.cssSelector("#index_message_detail_content")).getText();
        Assertions.assertNotNull(content);
        // × 按钮
        driver.findElement(By.cssSelector("#index_message_reply_modal > div > div > div.modal-header > button"));
        //取消按钮
        String cancelText = driver.findElement(By.cssSelector("#index_message_reply_cancel")).getText();
        Assertions.assertEquals("取消",cancelText);
        //回复按钮
        String reply = driver.findElement(By.cssSelector("#btn_index_message_reply")).getText();
        Assertions.assertEquals("回复",reply);

        //3、进行回复私信
        //3、1回复框和相关元素是否正确加载
        driver.findElement(By.cssSelector("#btn_index_message_reply")).click();
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#index_message_reply_div > div")));
        String reply1 = driver.findElement(By.cssSelector("#index_message_reply_receive_user_name")).getText();
        Assertions.assertTrue(reply1.contains("回复给:"));
        String reply2 = driver.findElement(By.cssSelector("#index_message_reply_receive_content")).
                getAttribute("placeholder");
        Assertions.assertEquals("请输入回复内容",reply2);
        String push = driver.findElement(By.cssSelector("#btn_index_send_message_reply")).getText();
        Assertions.assertEquals("发送",push);

        //3、2不输入内容直接回复
//        driver.findElement(By.cssSelector("#btn_index_send_message_reply")).click();
//        String noContent = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")).getText();
//        String[] s = noContent.split("\n");
//        Assertions.assertEquals("请输入要回复的内容",s[2]);
//        driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > " +
//                "span.close-jq-toast-single")).click();

        //3、3输入内容回复(与3、2分开来测)
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#index_message_reply_receive_content")));
        driver.findElement(By.cssSelector("#index_message_reply_receive_content")).sendKeys("你也好哇");
        driver.findElement(By.cssSelector("#btn_index_send_message_reply")).click();
        String success = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")).getText();
        String[] s1 = success.split("\n");
        Assertions.assertEquals("操作成功",s1[2]);

        //关闭私信列表
        driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div >" +
                " span.close-jq-toast-single")).click();
        driver.findElement(By.cssSelector("#index_message_offcanvasEnd > div.offcanvas-header > button")).click();
    }
}
